﻿using System;
using System.Data;
using System.Configuration;
using System.Text;
using System.IO;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;
using SSN.WebFramework;

namespace SSN.WebFramework.Controls
{
    public class HyperLink : System.Web.UI.WebControls.HyperLink
    {
        public LinkType LinkType { get; set; }
        public bool ShowIcon { get; set; }
        public string LinkText { get; set; }
        public bool NoCache { get; set; }

        public HyperLink()
        {
            ShowIcon = true;
        }

        protected override void Render(HtmlTextWriter writer)
        {
            string imgIcon = "";

            if (LinkType == LinkType.Auto)
            {
                string[] split = NavigateUrl.Split('?');

                if (split[0].StartsWith("http"))
                {
                    LinkType = LinkType.ExternalWebPage;
                    Target = "_blank";
                }
                else if (split[0].EndsWith("pdf") || split[0].EndsWith("getform.aspx") || split[0].EndsWith("getdocument.aspx"))
                {
                    LinkType = LinkType.Pdf;
                    NoCache = true;
                }
                else if (split[0].EndsWith("aspx"))
                {
                    LinkType = LinkType.InternalWebPage;
                }
                else if (split[0].EndsWith("doc") || split[0].EndsWith("docx"))
                {
                    LinkType = LinkType.WordDocument;
                    NoCache = true;
                }
                else if (split[0].EndsWith("xls") || split[0].EndsWith("xlsx"))
                {
                    LinkType = LinkType.ExcelSpreadsheet;
                    NoCache = true;
                }
                else if (split[0].EndsWith("ppt"))
                {
                    LinkType = LinkType.PowerPoint;
                    NoCache = true;
                }
                else if (split[0].EndsWith("mp3") || split[0].EndsWith("wmv"))
                {
                    LinkType = LinkType.Media;
                    NoCache = true;
                }
            }

            switch (this.LinkType)
            {
                case LinkType.Edoc:
                    imgIcon = "edoc.png";
                    break;

                case LinkType.ExcelSpreadsheet:
                    imgIcon = "excel.png";
                    break;

                case LinkType.ExternalWebPage:
                    imgIcon = "ie_ico.png";
                    break;

                case LinkType.InternalWebPage:
                    imgIcon = "page.png";
                    break;

                case LinkType.Pdf:
                    imgIcon = "pdf.png";
                    break;

                case LinkType.PowerPoint:
                    imgIcon = "pp.png";
                    break;

                case LinkType.WordDocument:
                    imgIcon = "word.png";
                    break;

                case LinkType.Media:
                    imgIcon = "media.png";
                    break;
            }



            if (NoCache)
            {
                if (NavigateUrl.Contains("?"))
                {
                    NavigateUrl = NavigateUrl + "&token=" + DateTime.Now.Ticks;
                }
                else
                {
                    NavigateUrl = NavigateUrl + "?token=" + DateTime.Now.Ticks;
                }
            }

            if (LinkType == LinkType.Pdf)
            {
                NavigateUrl = string.Format("javascript: SafePopUp('{0}', 600, 800)", NavigateUrl);
            }

            if (!string.IsNullOrEmpty(Text) && !string.IsNullOrEmpty(LinkText))
            {
                LinkText = "<span>" + LinkText + "</span>";
            }

            if (imgIcon.Length > 0 && ShowIcon)
            {
                if (!string.IsNullOrEmpty(LinkText))
                {
                    LinkText = string.Format("<img src='/images/icons/{0}' /> {1}", imgIcon, LinkText);
                }
                else
                {
                    Text = string.Format("<img src='/images/icons/{0}' /> {1}", imgIcon, Text);
                }
            }

            // basically if only text or linktext is set, whichever one is set becomes the Text.
            if (!string.IsNullOrEmpty(Text) && !string.IsNullOrEmpty(LinkText))
            {
                Text = LinkText + "<p>" + Text + "</p>";
            }
            else if (!string.IsNullOrEmpty(LinkText) && string.IsNullOrEmpty(Text))
            {
                Text = LinkText;
            }
            base.Render(writer);
        }
    }

    public enum LinkType
    {
        Auto,
        None,
        InternalWebPage,
        ExternalWebPage,
        Pdf,
        WordDocument,
        ExcelSpreadsheet,
        PowerPoint,
        Edoc,
        Media
    }
}
